package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    @Transactional
    @Override
    public void saveWithDishes(SetmealDto setmealDto) {
        //保存套餐基本信息
        save(setmealDto);
        //完善套餐id信息
        setmealDto.getSetmealDishes().forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
        });
        //保存菜品信息
        setmealDishService.saveBatch(setmealDto.getSetmealDishes());
    }

    @Override
    public Page<SetmealDto> selectByPage(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> pageInfo = new Page<>();
        //查询套餐的基本信息
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),Setmeal::getName,name);

        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        page(setmealPage,queryWrapper);

        //执行数据拷贝
        pageInfo.setTotal(setmealPage.getTotal());


        List<SetmealDto> recodes = new ArrayList<>();
        setmealPage.getRecords().forEach(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            Category category = categoryService.getById(setmeal.getCategoryId());
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
            //执行数据拷贝
            BeanUtils.copyProperties(setmeal,setmealDto);
            //把对象添加到集合
            recodes.add(setmealDto);
        });
        //把recodes设置到pageInfo中
        pageInfo.setRecords(recodes);
        return pageInfo;
    }

    @Override
    public SetmealDto updateWithDishes(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        //根据套餐id查询套餐信息
        return setmealDto;
    }

    @Override
    public void deleteSetmeal(Long[] ids) {
        //判断套餐状态是否为禁售
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.eq(Setmeal::getStatus,1).in(Setmeal::getId, Arrays.asList(ids));
        int count = this.count(setmealQueryWrapper);
        if (count>0){
            throw new CustomException("存在在售套餐不能删除");
        }
        //根据套餐id进行删除
        removeByIds(Arrays.asList(ids));
        //删除套餐菜品对应表
        for (Long setmealId : ids) {
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealId);
            setmealDishService.remove(setmealDishLambdaQueryWrapper);
        }
    }

    @Override
    public List<SetmealDishDto> listWithImage(Long setmealId) {
        //查询套餐对应的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        //查询dto集合
        ArrayList<SetmealDishDto> setmealDishDtos = new ArrayList<>();
        list.forEach(setmealDish -> {
            SetmealDishDto dto = new SetmealDishDto();
            //属性拷贝
            BeanUtils.copyProperties(setmealDish,dto);
            //查询图片
            String image = dishService.getById(setmealDish.getDishId()).getImage();
            dto.setImage(image);
            setmealDishDtos.add(dto);
        });
        return setmealDishDtos;
    }
}
